package com.ljkj.cordial.chat.ui;

import android.text.TextUtils;

import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.ims.IMSClientHelper;
import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.lanjiu.lib.imclient.user.UserPreferences;

/**
 * @description:
 * @author: Panwei
 * @date: 2019/11/19 22:24
 */
public abstract class IView {

    private ViewLoadingController mViewLoadingController;

    protected abstract void onMessageEventHandle(MessageEvent event);

    protected boolean checkIsSuccess(int code, String data) {
        return IMSContacts.ResponseCode.SUCCESS == code && !TextUtils.isEmpty(data);
    }

    public void doSendRequest(int action) {
        doSendRequest(action, null, false);
    }

    public void doSendRequest(int action, String params) {
        doSendRequest(action, params, false);
    }

    public void doSendRequestOnBackground(int action) {
        doSendRequest(action, null, true);
    }

    public void doSendRequestOnBackground(int action, String params) {
        doSendRequest(action, params, true);
    }

    private void doSendRequest(int action, String params, boolean isBackground) {
        if (!isBackground) {
            if (null != mViewLoadingController)
                mViewLoadingController.showLoading();
        }
        if (TextUtils.isEmpty(params)) {
            IMSClientHelper.getInstance().sendRequest(MessageEvent.createSenderEvent(action));
        } else {
            MessageEvent senderEvent = MessageEvent.createSenderEvent(action);
            senderEvent.setData(params);
            IMSClientHelper.getInstance().sendRequest(senderEvent);
        }
    }

    public void sendChatMessage(int action, String params) {
        if (TextUtils.isEmpty(params)) {
            IMSClientHelper.getInstance().sendMessage(MessageEvent.createSenderEvent(action));
        } else {
            MessageEvent senderEvent = MessageEvent.createSenderEvent(action);
            senderEvent.setData(params);
            IMSClientHelper.getInstance().sendMessage(senderEvent);
        }
    }

    public void onReadResponse(MessageEvent event) {
        if (null != mViewLoadingController)
            mViewLoadingController.hideLoading();
        onMessageEventHandle(event);
    }

    public void setViewLoadingController(ViewLoadingController viewLoadingController) {
        mViewLoadingController = viewLoadingController;
    }

    public interface ViewLoadingController {
        void showLoading();

        void hideLoading();
    }
}
